package de.fhluebeck.oop.gps.db;

import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.sql.DataSource;

/**
 * 
 * Diese Klasse stellt die physische Verbindung zur Datenbank her und 
 * verwaltet die Verbindungen zur Datenbank.
 * 
 * Der Zugriff auf die Datenbank erfolgt über diese Klasse
 * 
 * @author Ogün Bilge, Leif Hitzschke
 * @version 1.00, 13.11.2009
 * 
 */
public class BaseDataSource implements DataSource
{
  private int loginTimeout;
  private PrintWriter out;
  private String dbUrl;
  private String user;
  private String password;
  
  public BaseDataSource( String driver, String dbUrl, String user, String password )
  {
	  this.dbUrl = dbUrl;
	  this.user = user;
	  this.password = password;
    try
    {
      Class.forName( driver );
    }
    catch( Exception e )
    {
      System.out.println( "Database Driver not found: " + driver );
      System.exit( 1 );
    }
  }
    
  
  public void setLoginTimeout(int seconds) throws SQLException
  {
	  this.loginTimeout = seconds;
  }

  public void setLogWriter(java.io.PrintWriter out) throws SQLException
  {
	  this.out = out;
  }

  public int getLoginTimeout() throws SQLException
  {
    return this.loginTimeout;
  }

  public java.io.PrintWriter getLogWriter() throws SQLException
  {
    return this.out;
  }

  public Connection getConnection(String user, String password) throws SQLException
  {
    return DriverManager.getConnection( dbUrl, user, password );
  }

  public Connection getConnection() throws SQLException
  {
    return DriverManager.getConnection( dbUrl, user, password );
  }

    public Object unwrap(Class iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public boolean isWrapperFor(Class iface) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet.");
    }
  
}
